<?php 

namespace Zbsht\Controller;
use Think\Controller;
use Think\Verify;


class LoginController extends Controller{


	/**
	 * 登陆页面
	 * @return [type] [description]
	 */
	public function index(){

		if (IS_AJAX && IS_POST) {
			$post = I('post.');

			// print_r($post);exit;

			$checkVerify = $this->check_verify($post['vercode']);
			if ($checkVerify) {
				$where = [
					'username'=>$post['username'],
					'password'=>md5($post['password']),
				];

				$res = D('user')->where($where)->find();

				if (!$res) {
					$this->error('帐号密码错误');
				}
				if ($res['status']) {
					$this->error('帐号被锁定！');
				}else{
					$data = [
						'id'			=>	$res['id'],
						'last_time_ip'	=>	get_client_ip(),
						'last_time'		=>	time(),
					];
					M('user')->data($data)->save();


					session('uid' , $res['id']);
					session('username' , $res['username']);
					session('last_time_ip' , $res['last_time_ip']);
					session('last_time' , $res['last_time']);
					
					$this->success('登陆成功！', U('Zbsht/Index/index'));
				}
			}else{

				$this->error('验证码错误！');
			}
		}
		// import('')

		$this->display('login');
	}



	public function logout(){
		S('menu' . session(C('USER_AUTH_KEY')) , null);
		session(C('USER_AUTH_KEY'),null);
		session('username' , null);
		session('last_time_ip' , null);
		session('last_time' , null);

		R('Zbsht/Login/index');
	}



	/**
	 * 上传图片
	 * @return [type] [description]
	 */
	public function imagesUpdate(){
		$post = $_FILES;

		if (IS_POST && IS_POST) {
			$path = upload($post['file'] , 'imgHeader');

			if ($path['status'] == '-1') {
				return $this->error($path);
			}else{
				return $this->success('头像上传成功' , $path);
			}
		}
	}



	/**
	 * 成功验证码
	 * @return [type] [description]
	 */
	public function vercode(){
		$config  = [
			'fontSize' => 20,
			'length' => 1,
			'imageH' => 40,
			'imageW' => 140,
		];
		$Verify = new Verify($config);
		$Verify = $Verify->entry();
	}



	/**
	 * 验证 验证码是否正确
	 * @param  [type] $code [description]
	 * @param  string $id   [description]
	 * @return [type]       [description]
	 */
	public function check_verify($code , $id = ''){

		$verify = new Verify();
		return $res = $verify->check($code , $id);
	}



}












 ?>